$(function(){
	//初始化日期控件
  	$('#bt, #et').datetimepicker({
  		language: 'zh-CN',
	    format: 'yyyy-mm-dd',
	    autoclose: true,
	    minuteStep: 1,
	    minView: "month"
	});
});

//加载玩家礼物赠送记录数据
function loadDatas(s){
	
	if(s=="search"){
		$("#page").val("");
	}

	//页码
	var page = $("#page").val();
	//一页显示的条数
	var pageSize = 40;
	//开始时间
	var begin = $("#begin_time").val();
	//结束时间
	var end = $("#end_time").val();
	//筛选条件：1：玩家推广号； 2：玩家昵称；
	var sx = $("#sx").val();
	//输入的值
	var sxVal = $("#sxVal").val();
	//类型
	var type = $("#type").val();
	//礼物状态
	var state = $("#state").val();
	
	$.ajax({
		type: "post",
		data: {"page":page, "pageSize":pageSize, "begin":begin, "end":end, "sx":sx, "sxVal":sxVal, "type":type, "state":state},
		url: "../../../php/player/playerInfo/loadPlayerGiftSendData.php",
		beforeSend: function(){
			$("#load").css("display","block");
		},
		// error:function () {
		// 	alert('no');
		// },
		success: function(msg){

			$("#load").css("display","none");

			var msg=eval('(' + msg + ')');
			// alert(typeof (msg));
			//总金币
			$("#allScore").html(msg.countInfo[0].allScore);
			//银商上分
			$("#yssf").html(msg.countInfo[0].yssf);
			//银商下分
			$("#ysxf").html(msg.countInfo[0].ysxf);

			//表格数据
			var datas = msg.records;
			var html = "";
			if(datas.length == 0){
				html += "<tr><td colspan='10' align='center'>没查询到符合条件的数据~</td></tr>";
			}else{
				for(var i = 0; i<datas.length; i++){
					html += "<tr>";
					html += "<td><a style='color:blue' href='./playerDetailsInfo.php?gameId="+datas[i].sendGameId+"' title='查看赠送人详情'>"+datas[i].sendGameId+"</a></td>";//赠送推广号
					html += "<td><a style='color:blue' href='./playerDetailsInfo.php?gameId="+datas[i].sendGameId+"' title='查看赠送人详情'>"+datas[i].sendNickName+"</a></td>";//赠送昵称
					html += "<td><a style='color:blue' href='./playerDetailsInfo.php?gameId="+datas[i].getGameId+"' title='查看接收人详情'>"+datas[i].getGameId+"</a></td>";//接收推广号
					html += "<td><a style='color:blue' href='./playerDetailsInfo.php?gameId="+datas[i].getGameId+"' title='查看接收人详情'>"+datas[i].getNickName+"</a></td>";//接收昵称
					html += "<td>"+datas[i].sendScore+"</td>";//赠送金币
					html += "<td>"+datas[i].getScore+"</td>";//实际获得金币
					html += "<td>"+datas[i].sendTime+"</td>";//赠送时间
					html += "<td>"+datas[i].getTime+"</td>";//接收时间
					var state = datas[i].state;
					if(state == "0"){
						html += "<td>未领取</td>";
						
						//礼物ID
						var giftId = datas[i].presentID;
						//用户ID(赠送人ID)
						var sendGameId = datas[i].sendGameId;
						//对象ID(接收人ID)
						var getGameId = datas[i].getGameId;
						//道具ID
						var propertyID = datas[i].propertyID;
						//礼物数量
						var count = datas[i].presentCount;
						
						html += "<td><a href='javascript:void();' onclick='returnGift("+giftId+", "+sendGameId+", "+getGameId+", "+propertyID+", "+count+");'>退回</a></td>";//操作
					}else if(state == "1"){
						html += "<td>已领取</td>";
						html += "<td>-</td>";
					}else if(state == "2"){
						html += "<td>GM退回</td>";
						html += "<td>-</td>";
					}else if(state == "3"){
						html += "<td>送错</td>";
						html += "<td>-</td>";
					}
					
					html += "</tr>";
				}
			}
			$("#databody").html(html);
			
			//分页数据
			var pageInfos = msg.pageInfo;
			//总条数
			var allCount = pageInfos[0].count;
			$("#count").html(allCount);
			//总页数
			var allPage = Math.ceil(allCount/pageSize);
			if(allPage==0){
				allPage = 1;
			}
			//当前页码
			var now = pageInfos[0].page;
			
			$("#page").val(now);
			$("#nowPage").html(now);
			$("#allPage").html(allPage);
			$("#pageIpu").val("");
		}
	});
}

/*
 * 
 * 礼物退回操作
 * 参数：	presentId-礼物ID
		dwUserId-用户ID(赠送人ID)
		dwTargetId-对象ID(接收人ID)
		cbPropertyId-道具ID
		count-赠送数量
 * 
 */
// returnGift(undefined, 168512, 168511, 3, 1);
function returnGift(presentId, dwUserId, dwTargetId, cbPropertyId, count){
	
	$("#presentId").val(presentId);
	$("#dwUserId").val(dwUserId);
	$("#dwTargetId").val(dwTargetId);
	$("#cbPropertyId").val(cbPropertyId);
	$("#count").val(count);
	
	$("#returnGiftModal").modal("show");
	
}

//提交
function subReturn(){

	var presentId = $("#presentId").val();
	var dwUserId = $("#dwUserId").val();
	var dwTargetId = $("#dwTargetId").val();
	var cbPropertyId = $("#cbPropertyId").val();
	var count = $("#count").val();
	// alert("请求数据:"+presentId+000+"; "+dwUserId+"; "+dwTargetId+"; "+cbPropertyId+"; "+count);
	
	$.ajax({
		type: "post",
		data: {"presentId":presentId, "dwUserId":dwUserId, "dwTargetId":dwTargetId, "cbPropertyId":cbPropertyId, "count":count},
		url: "../../../php/player/playerInfo/returnGifts.php",
		beforeSend: function(){
			$("#load").css("display","block");
		},
		success: function(msg){
			$("#load").css("display","none");
			if(msg == "1"){
				$("#returnGiftModal").modal("hide");
				$("#tishi").modal("show");
			}else{
				alert(msg);
			}
		}
	});
}
//取消退回
function cannelReturn(){
	$("#presentId, #dwUserId, #dwTargetId, #cbPropertyId, #count").val("");
	$("#returnGiftModal").modal("hide");
}
